// *********************************************************
//      Copyright (c) Kosta Gaitanis
//      All rights reserved
//
//      k.gaitanis@gmail.com
//
// *********************************************************

#include "Cylinder.h"
#include "Trajectory.h"
const double PI_2 = 2*M_PI;

Trajectory::Trajectory(double radius, const QString &name) :
    Item(name)
{
    /* FIXME complete the code */
    // each segment is a cylinder
    double spacing = 5.0;  // the radial distance in degrees between two segments
    double rspacing = spacing / 2.0 / 180.0 * M_PI;  // the spacing in radians
    double r = 0.015*sqrt(radius);       // the radius of each segment (cylinder radius)
    double segmentLength = rspacing * radius; // arc length = radians * radius

    double theta;
    for (theta = 0.0; theta <= PI_2; theta += rspacing) {
        Cylinder *cyl = new Cylinder(r, segmentLength, "Cylinder");
        Matrix mTran = Matrix::translation(radius*cos(theta), radius*sin(theta), 0.0);
        Matrix mRot = Matrix::rotationX(M_PI/2.0)*Matrix::rotationY(theta);
        cyl->setTransform(mTran*mRot);
        cyl->getMaterial()->setDiffuse(Qt::yellow);
        cyl->getMaterial()->setAmbient(Qt::yellow);
        addChild(cyl);
    }

    // set up a material that glows in the dark
    getMaterial()->setAmbient(Qt::blue);
    getMaterial()->setDiffuse(Qt::yellow);
    getMaterial()->setEmission(Qt::red);

    /* STOP here */
}

